#include <iostream>
#include <memory>
#include "Route.hpp"
#include "ChatServer.hpp" // 网络通信的功能
#include "ThreadPool.hpp"

using namespace ThreadPoolModule;
using task_t = function<void()>;

// ./Chat_Server  port
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        std::cerr << "Usage: " << argv[0] << " port" << std::endl;
        return 1;
    }
    // std::string ip = argv[1];
    uint16_t port = std::stoi(argv[1]);
    Enable_Console_Log_Strategy();

    Route r;
    auto tp = ThreadPool<task_t>::GetInstance();

    //  bind 绑定函数，第一个参数为调用方法，第二个参数是实体，剩下的参数是传递到第一个参数的参数
    unique_ptr<UdpServer> udp = make_unique<UdpServer>(port,[&r,&tp]
        (int sockfd, const std::string& messages, InetAddr& addr){
            task_t t = std::bind(&Route::MessageRoute,&r, sockfd, messages, addr);
            tp->Enqueue(t);
    });

    udp->Init();
    udp->Start();

    return 0;
}